/**
 * Copyright WIPRO - MARVELL 2011
 */

package com.hisense.settings.widget;

import com.android.settings.R;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Class to create a toast for the given text and display the given icon along
 * with the toast.
 * @author Nandeeshwar
 * @date 2 June 2011
 */
public class ToastCreator {

    private static final String TAG = "ToastCreator";
    /** Variable to hold the text that will displayed in toast */
    private String mTextToDisplay;
    /** Reference to toast instance */
    private static Toast mToast;
    /** Reference to text view that displays the text */
    private TextView mTxtView;
    /** Variable to hold the toast display duration */
    private int mToastDuration = Toast.LENGTH_LONG;
    private static Object syn_obj = new Object();

    /**
     * Public constructor for the class
     * @param context
     *            the application context in which the toast will be displayed
     * @param displayText
     *            the text to be displayed in the toast
     * @param image
     *            the icon that will be displayed along with the toast
     */
    public ToastCreator(final Context context, final String displayText, final int image) {
        if (context == null) {
            Log.d(TAG, "Application context is NULL");
            return;
        }

        synchronized (syn_obj) {
            if (mToast != null)
                mToast.cancel();

            mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
            mTextToDisplay = displayText;

            mTxtView = new TextView(context);
            mTxtView.setTextColor(context.getResources().getColor(R.color.normal_text_color));
            mTxtView.setGravity(Gravity.CENTER);
            // mTxtView.setTextSize((float)
            // res.getDimension(R.dimen.toast_text_size));

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.toast_creator, null);
            ImageView iv = (ImageView) view.findViewById(R.id.toast_creator_imageview);
            iv.setBackgroundResource(image);
            mTxtView = (TextView) view.findViewById(R.id.toast_creator_textview);
            if (mToast != null) {
                mToast.setView(view);
                mToast.setGravity(Gravity.CENTER, 0, 0);
                mToast.setDuration(Toast.LENGTH_SHORT);
            }
        }
    }

    /**
     * Method to display the toast after setting all the values
     */
    public void showToast() {
        synchronized (syn_obj) {
            if (mToast != null) {
                mTxtView.setText("   " + mTextToDisplay.trim());
                mToast.setDuration(mToastDuration);
                mToast.show();
            }
        }
    }

    /**
     * Method to set the toast display duration
     * @param duration
     *            toast display duration
     */
    public void setToastDuration(final int duration) {
        mToastDuration = duration;
    }

    /**
     * Method to cancel the toast display
     */
    public static void cancelToast() {
        synchronized (syn_obj) {
            if (mToast != null) {
                mToast.cancel();
                mToast = null;
            }
        }
    }
}
